package com.zzty.daoimpl;

import com.zzty.common.DBUtilPool;
import com.zzty.dao.UserDao;
import com.zzty.entity.Clazz;
import com.zzty.entity.User;
import com.zzty.common.DBUtilPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    public void addUser(User user) throws Exception {
        Connection cn= DBUtilPool.getConnection();
        PreparedStatement pst=cn.prepareStatement("insert into user (name,password,type,email,img,clazzid)values (?,?,?,?,?,?)");
        pst.setString(1,user.getName());
        pst.setString(2,user.getPassword());
        pst.setString(3,user.getType());
        pst.setString(4,user.getEmail());
        pst.setString(5,user.getImg());
        pst.setString(6,String.valueOf(user.getClassId()));
        pst.executeUpdate();
        DBUtilPool.releaseConnection(cn);
    }

    public void deleteUser() throws Exception {

    }


    public String changeUser(User user) throws Exception {
        Connection cn= DBUtilPool.getConnection();
        PreparedStatement pst=cn.prepareStatement("update user set password=?,name=?,email=? where id=?");
        pst.setString(1,user.getPassword());
        pst.setString(2,user.getName());
        pst.setString(3,user.getEmail());
        pst.setString(4,String.valueOf(user.getId()));
        pst.executeUpdate();
        DBUtilPool.releaseConnection(cn);
        return "success";
    }


    public List<User> searchUser() throws Exception {
        List<User>users=new ArrayList<User>();
        Connection cn= DBUtilPool.getConnection();
        PreparedStatement pst=cn.prepareStatement("select * from user ");
        ResultSet rs=pst.executeQuery();
        while(rs.next()){
            User us=new User();
            us.setId(Integer.valueOf(rs.getString("id")));
            us.setName(rs.getString("name"));
            us.setPassword(rs.getString("password"));
            us.setType(rs.getString("type"));
            users.add(us);
        }
        DBUtilPool.releaseConnection(cn);
        return users;
    }

    public List<User> searchStudents() throws Exception {
        List<User>users=new ArrayList<User>();
        Connection cn= DBUtilPool.getConnection();
        PreparedStatement pst=cn.prepareStatement("select * from user where type=?");
        pst.setString(1,"student");
        ResultSet rs=pst.executeQuery();
        while(rs.next()){
            User us=new User();
            us.setId(Integer.valueOf(rs.getString("id")));
            us.setName(rs.getString("name"));
            us.setPassword(rs.getString("password"));
            us.setType(rs.getString("type"));
            users.add(us);
        }
        DBUtilPool.releaseConnection(cn);
        return users;
    }
    public User searchUserByName(String name) throws Exception {
        Connection cn= DBUtilPool.getConnection();
        PreparedStatement pst=cn.prepareStatement("select * from user where name=?");
        pst.setString(1,name);
        ResultSet rs=pst.executeQuery();
        User us=new User();
        while(rs.next()){
            us.setId(Integer.valueOf(rs.getString("id")));
            us.setName(rs.getString("name"));
            us.setType(rs.getString("type"));
        }
        DBUtilPool.releaseConnection(cn);
        return us;
    }
    public User searchUserById(Integer id) throws Exception {
        Connection cn= DBUtilPool.getConnection();
        PreparedStatement pst=cn.prepareStatement("select * from user where id=?");
        pst.setString(1,String.valueOf(id));
        ResultSet rs=pst.executeQuery();
        User user=new User();
        while(rs.next()){
            user.setName(rs.getString("name"));
            user.setClassId(Integer.valueOf(rs.getString("clazzid")));
            user.setId(Integer.valueOf(rs.getString("id")));
            user.setPassword(rs.getString("password"));
            user.setEmail(rs.getString("email"));
            user.setType(rs.getString("type"));
        }
        DBUtilPool.releaseConnection(cn);
        return user;
    }

    public List<Clazz>searchClazz() throws Exception{
        List<Clazz>clazz=new ArrayList<Clazz>();
        Connection cn= DBUtilPool.getConnection();
        PreparedStatement pst=cn.prepareStatement("select * from clazz");
        ResultSet rs=pst.executeQuery();
        while(rs.next()){
            Clazz cl=new Clazz();
            cl.setId(Integer.valueOf(rs.getString("id")));
            cl.setName(rs.getString("name"));
            clazz.add(cl);
        }
        DBUtilPool.releaseConnection(cn);
        return clazz;
    }
}
